package com.yangrui.util;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.sql.*;
import java.util.Properties;

/**
 * 封装多余jdbc代码
 */
public class JdbcUtil {
    public static Connection jdbc() throws ClassNotFoundException, IOException, SQLException {
        Class.forName("com.mysql.jdbc.Driver");

        //读取配置文件
        Properties properties = new Properties();
        properties.load(Files.newInputStream(Paths.get("src/main/resources/mysql.properties")));
        String url = properties.getProperty("url");
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        //返回一个Connection
        return DriverManager.getConnection(url, user, password);
    }

    //释放资源方法
    public static void close(Connection con, Statement stat) {
        if (con != null) {
            try {
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if (stat != null) {
            try {
                stat.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

}
